using FileTransferDesktop.ViewModels.Dialogs;
using DataFormats = System.Windows.DataFormats;
using DragDropEffects = System.Windows.DragDropEffects;
using DragEventArgs = System.Windows.DragEventArgs;

namespace FileTransferDesktop.Views.Dialogs;

public partial class UploadDialog
{
    public UploadDialogViewModel ViewModel { get; }

    public UploadDialog(UploadDialogViewModel viewModel)
    {
        ViewModel = viewModel;
        DataContext = ViewModel;
        InitializeComponent();
    }

    private void OnCancelClicked(object sender, RoutedEventArgs e)
    {
        Hide();
    }

    #region 拖拽事件处理

    private void OnDragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effects = DragDropEffects.Copy;

            // 添加视觉反馈
            if (sender is FrameworkElement element)
            {
                element.Opacity = 0.8;
            }
        }
        else
        {
            e.Effects = DragDropEffects.None;
        }
        e.Handled = true;
    }

    private void OnDragOver(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effects = DragDropEffects.Copy;
        }
        else
        {
            e.Effects = DragDropEffects.None;
        }
        e.Handled = true;
    }

    private void OnDragLeave(object sender, DragEventArgs e)
    {
        // 恢复视觉状态
        if (sender is FrameworkElement element)
        {
            element.Opacity = 1.0;
        }
        e.Handled = true;
    }

    private void OnDrop(object sender, DragEventArgs e)
    {
        try
        {
            // 恢复视觉状态
            if (sender is FrameworkElement element)
            {
                element.Opacity = 1.0;
            }

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files != null && files.Length > 0)
                {
                    ViewModel.AddDroppedFiles(files);
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"拖拽处理错误: {ex.Message}");
        }

        e.Handled = true;
    }

    #endregion
}
